package cst.a931xmc.yuejian20.Adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cst.a931xmc.yuejian20.Bean.CommentBean;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.R;
import de.hdodenhof.circleimageview.CircleImageView;

public class CommentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<CommentBean> list;
    private Context context;

    public CommentAdapter(Context context){
        inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public void setList(List<CommentBean> l){
        list = l;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v = inflater.from(parent.getContext()).inflate(R.layout.item_comment, parent, false);
        RecyclerView.ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        CommentBean bean = list.get(position);
        if(bean == null)
            return;
        final ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.user_name.setText(bean.getUser_name());
        viewHolder.comment_time.setText(bean.getComment_time());
        viewHolder.comment_intro.setText(bean.getComment_intro());
        ApiModel apiModel = new ApiModel();
        Picasso.with(context)
                .load(apiModel.getBaseUrl() + "static/posters/" + bean.getUser_pic())
                .placeholder(R.drawable.nopic)
                .into(viewHolder.user_pic);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        if (list == null) return 0;
        else return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        CircleImageView user_pic;
        TextView user_name;
        TextView comment_time;
        TextView comment_intro;

        public ViewHolder(View itemView) {
            super(itemView);
            user_pic = itemView.findViewById(R.id.user_pic);
            user_name = itemView.findViewById(R.id.user_name);
            comment_time = itemView.findViewById(R.id.comment_time);
            comment_intro = itemView.findViewById(R.id.comment_intro);
        }
    }
}
